<?

//Data loading
$title='KWITANSI';
$title2='RECEIPT';
if (GetParam("office","")) $perush = GetCompanyName();
else $perush = 'PT. MASAJI PRAYASA CARGO';
$page   = 'PAGE';



$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter"); //array("239.4","138.6")
//$pdf=new PDFReport("P","mm",array("239.4","138.6")); //array("239.4","138.6")
$pdf->AddPage();
$Result = GridList (GetParam("nokwit",""));
$pdf->GridTable($Result);
$pdf->Output();



function GridList ($TransNo) {
    global $DBConnection, $DBName2;
      $Field = ReadHeader($TransNo);
      global $DecimalPoint;
      $DecimalPoint = ($Field->curr<>"IDR")?2:0;
//    print $DecimalPoint;die();
      $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

    if ($Field->noppn==1) {
            //$DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $SQL  = "SELECT ais_receipt.*, if (ifnull(tblreceipt.length,0)=0,'',tblreceipt.length) as size, if (tblcoa.inc_ppn=1,ais_receipt.jumlah*110/100,ais_receipt.jumlah) as jumlah FROM ais_receipt ";
                $SQL .= "LEFT JOIN tblreceipt ON ais_receipt.thbl=tblreceipt.kdreceipt ";
                $SQL .= "LEFT JOIN tblcoa ON ais_receipt.skada=tblcoa.skada ";
        $SQL .= "WHERE nokwit='".GetParam("nokwit","")."' AND ais_receipt.skada <> '".GetPPNSkada()."' GROUP BY receipt_id ORDER BY urut ";
    } else {

            //$DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $SQL  = "SELECT ais_receipt.*, if (ifnull(tblreceipt.length,0)=0,'',tblreceipt.length) as size FROM ais_receipt ";
                $SQL .= "LEFT JOIN tblreceipt ON ais_receipt.thbl=tblreceipt.kdreceipt ";
        $SQL .= "WHERE nokwit='".GetParam("nokwit","")."' ORDER BY urut ";
    }
    //die($SQL);
    return $DBConnection->dbc->get_results($SQL,ARRAY_A);
}

function ReadHeader($nokwit) {
  global $DBConnection;
  $SQL = "
            SELECT *,DATE_FORMAT(tglkwit,'%d %M, %Y') as spell_date, SUM(jumlah)  as TOTALPAID,
            if (ifnull(tbldebtur.CUNPWP,'')='','-',tbldebtur.CUNPWP) as CUNPWP
            FROM ais_receipt
            LEFT JOIN tbldebtur ON ais_receipt.debtur=tbldebtur.CUCODE
            WHERE nokwit='$nokwit'
            GROUP BY nokwit
        ";
  return $DBConnection->dbc->get_row($SQL);
}

class PDFReport extends PDF {

function Header() {
    global $title,$title2,$perush,$page,$DEFINE_FONT,$DBConnection;
    if ($DEFINE_FONT==0) {
        $this->AddFont('Arial','','arialn.php');
        $DEFINE_FONT=1;
    }
    $Field = ReadHeader(GetParam("nokwit",""));
        $this->spell_date = strtoupper($Field->spell_date);
        list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

        $this->SetFont('Arial','',12);
        $this->SetLeftMargin(5);
//    $this->Cell(200,5,"",0,1,'L',0);
//    $this->Ln();$this->Ln();$this->Ln();
        $this->Cell(82,5,$perush,0,0,'L',0);
        $this->SetFont('Arial','',16);
if (strlen($Field->CUNPWP)>1 and substr($Field->CUNPWP,0,1)=='0') {
        $this->Cell(90,5,$title." / ".$title2,0,1,'C',0);
} else {
        $this->Cell(90,5,$title." / ".$title2." / FAKTUR PAJAK SEDERHANA" ,0,1,'C',0);
}

    //$this->Cell(155,5,"",0,0,'C',0);
      //$this->Ln();
      //$this->Cell(5,5,"",0,0,'L',0);
    $this->SetFont('Arial','',10);
    $this->Cell(5,5,"",0,0,'L',0);
    $this->Cell(12,5,"Alamat : ",0,0,'L',0);
    $this->Cell(138,5,"JL. KALIBARU NO.4, KALIBARU, CILINCING, JAKARTA UTARA",0,0,'L',0);
    $this->SetFont('Arial','',12);
        $this->Cell(30,5,"NO: ".GetParam("nokwit",""),0,1,'L',0);
    $this->SetFont('Arial','',10);
    $this->Cell(5,5,"",0,0,'L',0);
    $this->Cell(12,5,"NPWP  : ",0,0,'L',0);
    $this->Cell(138,5,"01.577.508.3-046.000",0,0,'L',0);
    $this->Cell(30,5,GetOfficeLocation(),0,0,'L',0);

    //Rama, (09 September 2009), Start
    $SQL = "SELECT flag_original_print FROM ais_receipt WHERE nokwit = '".GetParam("nokwit","")."'";
    $GetFlag = $DBConnection->dbc->get_var($SQL);
    //echo "$GetFlag"; exit;
    if ($GetFlag == "0") $this->Cell(30,5,"(Original)",0,0,'L',0);
    else $this->Cell(30,5,"(Copy)",0,0,'L',0);
    $SQL = "UPDATE ais_receipt SET flag_original_print= '1' WHERE nokwit = '".GetParam("nokwit","")."'";
    $DBConnection->dbc->query($SQL);
    //Rama, (09 september 2009), End
      $this->Ln();

//    $this->Cell(50,5,GetOfficeLocation(),0,0,'L',0);

//  $this->Cell(50,5,"NO ORDER: ",0,1,'L',0);
//  $this->Ln();
    $this->SetLeftMargin(5);
//  $this->Line(5,30,210,30);
      $this->SetFont('Arial','',12);
    $this->Cell(50,5,"SUDAH TERIMA DARI / RECEIVED FROM","T",0,'L',0);
    $this->Cell(3,5,":","T",0,'L',0);
    $this->Cell(70,5,"","TR",0,'L',0);
    $this->Cell(80,5,"ADDRESS :","T",0,'L',0);
    $this->Ln();
    $this->Cell(50,5,$Field->debnam,"",0,'L',0);
    $this->Cell(3,5,"","",0,'L',0);
    $this->Cell(70,5,"","",0,'L',0);
    $this->MultiCell(80,5,$Field->CUADDR,"L","");

if (strlen($Field->CUNPWP)>1 and substr($Field->CUNPWP,0,1)=='0') {
//    $this->Ln();
    $this->Cell(50,5,"NPWP","",0,'L',0);
      $this->Cell(3,5,":","",0,'L',0);
    $this->Cell(70,5,$Field->CUNPWP,"R",0,'L',0);
//  $this->Cell(62,5,"","R",0,'L',0);
      $this->Cell(80,5,"","",0,'L',0);
    $this->Ln();
} else {
//    $this->Ln();
    $this->Cell(123,5,"","R",0,'L',0);
    $this->Cell(80,5,"","",0,'L',0);
    $this->Ln();

}
    $this->Cell(50,5,"OPERATOR CONTAINER :","B",0,'L',0);
      $this->Cell(3,5,":","",0,'L',0);
    $this->Cell(70,5,$Field->principal,"BR",0,'L',0);
    $this->Cell(80,5,"","B",0,'L',0);

//    $this->Ln();
    $this->Ln();


    $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_receipt WHERE ais_receipt.nokwit='".GetParam("nokwit","")."' AND tblcurrency.tusymbol=ais_receipt.curr AND ktrasl='".GetOfficeID()."'");
    //$DBConnection->dbc->debug();

/*
    $cc = new intToRupiah;
    $cc->setTail($GetCurrency);
    $cc->setNumber($Field->TOTALPAID);
    $SpellNumber = strtoupper(trim($cc->currency));
*/

    $cc = new numtoword;
    if ($Field->curr<>"IDR") {
        list($number,$decimal) = explode(".",$Field->TOTALPAID);
        if ($decimal==0) $Field->TOTALPAID = $number;
    } else $Field->TOTALPAID = round($Field->TOTALPAID);
    $SpellNumber = strtoupper(trim($cc->en($Field->TOTALPAID)));

    $this->Cell(203,5,"BANYAKNYA UANG ","T",0,'L',0);
    $this->Ln();
    $this->Cell(203,2,"-------------------------------- :  ".$SpellNumber." ".$Field->curr,"",0,'L',0);
    $this->Ln();
    $this->Cell(203,5,"THE SUM OF ","",0,'L',0);
    $this->Ln();
//    $this->Ln();
    //Header
    //Column widths

    $w=array(15,73,30,10,10,35,30);
    $this->SetFont('Arial','',12);
    $this->Cell($w[0],5,"NO.",1,0,'C',0);
    $this->Cell($w[1],5,"URAIAN / PEMBAYARAN",1,0,'C',0);
        $this->Cell($w[2],5,"LATTER NO",1,0,'C',0);
    $this->Cell($w[3],5,"QTY",1,0,'C',0);
        $this->Cell($w[4],5,"SIZE",1,0,'C',0);
        $this->Cell($w[5],5,"PRICE",1,0,'C',0);
    $this->Cell($w[6],5,"JUMLAH",1,0,'C',0);
    $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths
            global $DBConnection;
    $w=array(15,73,30,10,10,35,30);
        //Data
        if ($MyResult)
//echo '<pre>';var_dump($MyResult);die('</pre>');
        foreach($MyResult as $row)
        {
                $Total = $Total + $row["jumlah"];
            $this->Cell($w[0],5,$row["urut"].".",'LR',0,'R');
            $this->Cell($w[1],5,$row["uraian"],'LR',0,'L');
            $LatterNoArray = explode(';',trim($row["container_no"]));
            $this->Cell($w[2],5,$LatterNoArray[0],'LR',0,'L');

                    $this->Cell($w[3],5,$row["jenis"],'LR',0,'R');
                    $this->Cell($w[4],5,$row["size"],'LR',0,'C');
                    if ($row["curr"]<>"IDR") $decPoint = 2; else $decPoint = 0;
                    $this->Cell(10,5,$row["curr"],'LR',0,'C');
                    $this->Cell($w[5]-10,5,FormatNumber($row["dol"],$decPoint),"LR",0,R);
                    $this->Cell($w[6],5,FormatNumber($row["jumlah"],$decPoint),'LR',0,'R');
            $this->Ln();
            if (count($LatterNoArray)>1) {
             for ($x=1;$x<count($LatterNoArray);$x++) {
                $this->Cell($w[0],5,"",'LR',0,'R');
                $this->Cell($w[1],5,"",'LR',0,'L');
                $this->Cell($w[2],5,$LatterNoArray[$x],'LR',0,'L');
                        $this->Cell($w[3],5,"",'LR',0,'R');
                        $this->Cell($w[4],5,"",'LR',0,'C');
                        $this->Cell(10,5,"",'LR',0,'C');
                        $this->Cell($w[5]-10,5,"","LR",0,R);
                        $this->Cell($w[6],5,"",'LR',0,'R');
              $this->Ln();
             }
          }

        }
            $cc = new intToRupiah;
            $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_advance WHERE ais_advance.nobkt='".GetParam("nobkt","")."' AND tblcurrency.tusymbol=ais_advance.curr AND ktrasl='".GetOfficeID()."'");
            //$DBConnection->dbc->debug();
            $cc->setTail($GetCurrency);
            $cc->setNumber($Total);
            $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5],5,"TOTAL:",1,0,'R',0);
                $this->Cell($w[6],5,FormatNumber($Total,$decPoint),1,0,'R',0);
                $this->Ln();

                //$this->Cell(15,5,"VAT NUMBER : ".$row["faktur"],0,0,'L',0);
                $this->Cell(50,5,"",0,0,'L',0);
                $this->Cell(140,5,"",0,0,'L',0);
                $this->Ln();

                $this->Cell(15,5,"REMARK : ",0,0,'L',0);$this->Cell(50,5," ",0,0,'L',0);
                $this->Cell(90,5,"",0,0,'L',0);
                $this->Cell(50,5,GetBranchName().", ".$this->spell_date,0,1,'C',0);

                $this->Cell(15,5,"",0,0,'L',0);$this->MultiCell(120,5,$row["remark1"],0,1,'L',0);
                //MultiCell(80,5,$Field->CUADDR,"L","")

                $this->Ln();
                        $SQL = "SELECT * FROM $DBName.tblprofile WHERE ktrasl='".GetOfficeID()."'";
                    $Result = $DBConnection->dbc->get_row($SQL);

                $this->Cell(155,5,"",0,0,'L',0);$this->Cell(50,5,"( ".$Result->ttdkw." )",0,1,'C',0);

//              $this->Ln();
//              $this->Ln();
//Closure line
    }

    function Footer() {
        //Closure line
        //$this->Cell(205,0,'','T');
    }

}

?>
